package de.breitbeil.samsyncro.lib.domain.parser;

import java.util.ArrayList;

import de.breitbeil.samsyncro.lib.domain.pim.SVCard;
import de.breitbeil.samsyncro.lib.util.Util;

public class SVCardParser {

    public static ArrayList<SVCard> parseList(String s) throws Exception {
        final String delimiter = "END:VCARD\r\n";
        ArrayList<SVCard> result = new ArrayList<SVCard>();
        String[] vcards = s.split(delimiter);
        
        for(int i = 0; i < vcards.length; i++) {
            String vcard = vcards[i];
            byte[] id = new byte[2];
            vcard.getBytes(0, 2, id, 0);

            // TODO: What are byte 2 and 3?
            
            String v = vcard.substring(4) + "END:VCARD";
            
            SVCard svcard = new SVCard();
            svcard.setId( Util.unsingnedWordToInt(id) );
            svcard.setVcard(v);
            
            result.add(svcard);
        }
        
        return result;
    }
    
}
